package com.applink.sell_proof.database.table;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;

import com.applink.sell_proof.EMarketing;
import com.applink.sell_proof.database.EMarketingContentProvider;
import com.applink.sell_proof.model.NewRewardClaim;
import com.applink.sell_proof.pojo.GenericAttribute;
import com.applink.sell_proof.pojo.GenericObject;
import com.applink.sell_proof.share.Parameter;
import com.applink.sell_proof.utilXMLParser.ObjPropInfo;

public class RewardClaimDBHelper {
public static final String TABLE_NAME = "reward_claim_table";
	
	public static final String RW_ID = "RewardID";
	//public static final String RW_OBJECT_ID = "RewardClaimID";
	public static final String RW_CL_ID = "ProgramID";
	public static final String RW_CL_NAME = "ProgramName";
	public static final String RW_CL_QUARTER = "Quarter";
	public static final String RW_CL_YEAR = "Year";
	public static final String RW_CL_PROGRAM_CODE = "ProgramCode";
	public static final String RW_CL_CATEGORY_ID = "ProductCategoryID";
	public static final String RW_CL_CATEGORY = "ProductCategoryName";
	public static final String RW_CL_REWARD_AMOUNT = "TotalRewardAmount";
	public static final String RW_CL_TOTAL_QUANTITY = "TotalQuantity";
	public static final String RW_CL_TOTAL_PERIOD = "Period";
	
	public static final String SQL_EXCUTE = "CREATE TABLE " + TABLE_NAME + "("
			+ RW_ID + " integer primary key ," 
			+ RW_CL_CATEGORY_ID + " text," 
			+ RW_CL_ID + " text," 
			+ RW_CL_NAME + " text, " 
			+ RW_CL_QUARTER + " text ," 
			+ RW_CL_YEAR + " text ,"
			+ RW_CL_PROGRAM_CODE + " text ," 
			+ RW_CL_CATEGORY + " text ," 
			+ RW_CL_REWARD_AMOUNT + " text ," 
			+ RW_CL_TOTAL_PERIOD + " text ,"
			+ RW_CL_TOTAL_QUANTITY + " text " + ");";
	
	public static void createRewardClaim(final NewRewardClaim reward) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				ContentValues value = new ContentValues();
				ContentResolver resolver = EMarketing.getInstance()
						.getApplicationContext().getContentResolver();
				value.put(RW_ID, reward.getIdClaim());
				value.put(RW_CL_CATEGORY_ID, reward.getCategoryId());
				value.put(RW_CL_ID, reward.getProgramID());
				value.put(RW_CL_NAME, reward.getProgramName());
				value.put(RW_CL_QUARTER, reward.getQuarter());
				value.put(RW_CL_YEAR, reward.getYear());	
				value.put(RW_CL_PROGRAM_CODE, reward.getProgramCode());
				value.put(RW_CL_CATEGORY, reward.getProductCategoryName());
				value.put(RW_CL_REWARD_AMOUNT, reward.getTotalRewardAmount());
				value.put(RW_CL_TOTAL_QUANTITY, reward.getTotalQuantity());	
				value.put(RW_CL_TOTAL_PERIOD, reward.getPeriod());	
				resolver.insert(EMarketingContentProvider.REWARD_CLAIM_CONTENT_URI,
						value);
			}

		}.start();
	}
	
	public static void clearRewardClaim(){
		ContentResolver resolver = EMarketing.getInstance()
				.getApplicationContext().getContentResolver();
		resolver.delete(EMarketingContentProvider.REWARD_CLAIM_CONTENT_URI, null, null);
	}
	
	@SuppressWarnings("rawtypes")
	public static ArrayList<GenericObject> getObjectBasedConfig(ArrayList<ObjPropInfo> objPropList) {
		
		if(objPropList.size() ==0){
			return null;
		}
		
		ArrayList<GenericObject> genericObjectList = new ArrayList<GenericObject>();
		  			
	        try {
	        	//whole items in objPropList must have the same om_id since they belong to an unique Generic Object
	        	//at here we fetch data of each record based on ID of each property
	        	
	        	ContentResolver resolver = EMarketing.getInstance().getApplicationContext().getContentResolver();
	        
	        	String [] queriedColumns = new String [objPropList.size()];
	        	for (int i=0;i < objPropList.size();i++)
	        		queriedColumns[i] = objPropList.get(i).getOPID();

	        	Cursor cursor = resolver.query(
	        			EMarketingContentProvider.REWARD_CLAIM_CONTENT_URI, queriedColumns, null,
	    				null, null);
	        	
	            if( cursor !=null) {

	            	while(cursor.moveToNext()){	
	            		
	            		//create new genericObject
	            		GenericObject genericObject = new GenericObject();
	            		genericObject.setObjectName(objPropList.get(0).getOm());
	            		
	            		ArrayList<GenericAttribute> attributeList = new ArrayList<GenericAttribute>();
                		
	            		for(int j=0; j < objPropList.size() ; j++){ //the number of attributes of the record must be the same with objPropList's size
	                		
	            			ObjPropInfo objPropInfo = objPropList.get(j);
	                		
	                		String name = objPropInfo.getOPID();
	                		String dataType = objPropInfo.getDataType();
	                			                		
	                		if(dataType.compareTo(Parameter.TYPE_INT) == 0) {
	                			GenericAttribute<String, Integer> attribute = new GenericAttribute<String, Integer>(name, (int) cursor.getLong(cursor.getColumnIndex(name)));
	                			attributeList.add(attribute);
	                		} else if (dataType.compareTo(Parameter.TYPE_CHAR) == 0){
	                			GenericAttribute<String, String> attribute = new GenericAttribute<String, String>(name, cursor.getString(cursor.getColumnIndex(name)));
	                			attributeList.add(attribute);
	                		} else if (dataType.compareTo(Parameter.TYPE_DOUBLE) == 0){
	                			GenericAttribute<String, Double> attribute = new GenericAttribute<String, Double>(name, (double)cursor.getDouble(cursor.getColumnIndex(name)));           			
	                			attributeList.add(attribute);
	                		}
	                		//if you have more datatype, please define here
	                	}
	            		
	            		genericObject.setAttributeList(attributeList);
	            		genericObjectList.add(genericObject);	                

	            	}
	            }
	        } catch (SQLException e) {
	        	genericObjectList = null;
	        }
   	
    	return genericObjectList;
	}
}
